﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class AsyncSpriteLoader : MonoBehaviour
{
    public string sprite_path;
    public SpriteRenderer render;
    public Image image;

    public UnityEngine.Events.UnityAction callback = null;

    private bool _loaded = false;

    public bool Loaded
    {
        get { return _loaded; }
    }

	// Use this for initialization
	void Start () 
    {
        if (render != null)
            render.gameObject.SetActive(false);

        if (image != null)
            image.gameObject.SetActive(false);
        this.RefreshSprite();
	}
	
	// Update is called once per frame
	void Update () {
	    
	}

    void RefreshSprite()
    {
//        if (render != null && sprite_path != null)
//        {
//            _loaded = false;
//            SpriteCache.Instance.GetCacheObjectAsync(sprite_path, (Sprite sprite, string path) =>
//            {
//                if (this != null && sprite_path.Equals(path))
//                {
//                    _loaded = true;
//                    if (sprite != null)
//                    {
//                        render.sprite = sprite;
//                        render.gameObject.SetActive(true);
//                    }
//                    
//                    if (callback != null)
//                    {
//                        callback();
//                    }
//                }
//            });
//        }
//
//        if (image != null && sprite_path != null)
//        {
//            _loaded = false;
//            SpriteCache.Instance.GetCacheObjectAsync(sprite_path, (Sprite sprite, string path) =>
//            {
//                if (this != null && sprite_path.Equals(path))
//                {
//                    _loaded = true;
//                    if (sprite != null)
//                    {
//                        image.SetSprite(sprite);
//                        image.gameObject.SetActive(true);
//                    }
//                    
//                    if (callback != null)
//                    {
//                        callback();
//                    }
//                }
//            });
//        }
    }

//    public void RefreshSprite(string path)
//    {
//        sprite_path = ResourcesEx.ResourcePath(path);
//        this.RefreshSprite();
//    }
}
